<template>
  <div class="content">
    <!-- <el-row :gutter="20">
      <el-col :span="12" :offset="6">
        <div class="grid-content bg-purple">
          <el-upload
            id="icon"
            :limit="1"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
            action="https://jsonplaceholder.typicode.com/posts/"
            list-type="picture-card"
            accept="image/jpg, image/jpeg, image/png"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <span style="color:#ccc; font-size: 14px;">{{ $t('dashboard.upload') }}</span>
          </el-upload>
          <span style="color: #333; font-size: 12px;">{{ $t('dashboard.impose') }}</span>
        </div>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="ipt">
      <el-col :span="10" :offset="6">
        <div class="grid-content bg-purple">
          <el-form ref="form" :rules="rules" :model="form">
            <el-form-item :label-width="formLabelWidth" :label="$t('dashboard.userName')" >
              <el-input v-model.trim="form.name" disabled style="width:90%"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="Email" >
              <el-input v-model.trim="form.Email" disabled style="width:90%"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" :label="$t('dashboard.oldpass')" prop="oldpass" >
              <el-input v-model.trim="form.oldpass" :placeholder="$t('dashboard.oldpass')" style="width:90%"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" :label="$t('dashboard.newpass')" prop="newpass" >
              <el-input :maxlength="18" v-model.trim="form.newpass" :placeholder="$t('dashboard.rule')" style="width:90%"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" :label="$t('dashboard.confirmpass')" prop="surepass" >
              <el-input :maxlength="18" v-model.trim="form.surepass" :placeholder="$t('dashboard.rule')" style="width:90%"/>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
    <el-row type="flex" class="row-bg" justify="center">
      <el-col :span="6">
        <div class="grid-content bg-purple">
          <el-button type="primary" @click="submit">{{ $t('dashboard.submit') }}</el-button>
        </div>
      </el-col>
    </el-row> -->
    <span>{{ $t('dashboard.personalInformation') }}</span>
    <el-divider/>
    <el-row :gutter="20">
      <el-col :span="6" style="text-align: center;">
        <div class="grid-content bg-purple">
          <el-upload
            id="icon"
            :limit="1"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
            action="https://jsonplaceholder.typicode.com/posts/"
            list-type="picture-card"
            accept="image/jpg, image/jpeg, image/png"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <span style="color:#ccc; font-size: 14px;">{{ $t('dashboard.upload') }}</span>
          </el-upload>
          <span style="color: #333; font-size: 12px;">{{ $t('dashboard.impose') }}</span>
        </div>
      </el-col>
      <el-col :span="12">
        <div class="grid-content bg-purple">
          <el-form ref="form" :rules="rules" :model="form">
            <el-form-item :label-width="formLabelWidth" :label="$t('dashboard.userName')" prop="name">
              <el-input v-if="showInput" v-model.trim="form.name" style="width:300px"/>
              <span v-if="!showInput" style="width:300px;display: inline-block;">{{ form.name }}</span>
              <el-button v-if="!showInput" type="text" style="margin-left: 10px;" @click="edit(1)">{{ $t('dashboard.modify') }}</el-button>
              <el-button v-if="showInput" type="text" style="margin-left: 10px;" @click="edit(2)">{{ $t('dashboard.cancel') }}</el-button>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="Email" >
              <el-input v-model.trim="form.Email" disabled style="width:300px"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" :label="$t('dashboard.oldpass')" prop="oldpass" >
              <el-input v-model.trim="form.oldpass" :placeholder="$t('dashboard.oldpass')" style="width:300px"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" :label="$t('dashboard.newpass')" prop="newpass" >
              <el-input :maxlength="18" v-model.trim="form.newpass" :placeholder="$t('dashboard.rule')" style="width:300px"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" :label="$t('dashboard.confirmpass')" prop="surepass" >
              <el-input :maxlength="18" v-model.trim="form.surepass" :placeholder="$t('dashboard.rule')" style="width:300px"/>
            </el-form-item>
            <el-form-item style="text-align: center;width:80%">
              <el-button type="primary" @click="submit('form')">{{ $t('dashboard.submit') }}</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import bg from '../../assets/b.png'
export default {
  name: 'AccoutSettings',
  data() {
    return {
      imageUrl: bg, // 头像
      rules: { // 规则
        name: [
          { required: true, message: this.$t('dashboard.sureoldpass'), trigger: 'blur' }
        ],
        oldpass: [
          { required: true, message: this.$t('dashboard.sureoldpass'), trigger: 'blur' }
        ],
        newpass: [
          { required: true, message: this.$t('dashboard.enterpass'), trigger: 'blur' }
        ],
        surepass: [
          { required: true, message: this.$t('dashboard.confirmpassA'), trigger: 'blur' }
        ]
      },
      formLabelWidth: '200px',
      form: {
        name: 'sjj 自建项目',
        Email: '1111111@163.com',
        oldpass: '123456',
        newpass: '',
        surepass: ''
      },
      showInput: false // 显示用户名input
    }
  },
  methods: {
    // 成功
    handleAvatarSuccess(res, file) {
      const self = this
      self.imageUrl = URL.createObjectURL(file.raw)
    },
    // 限定图片上传格式
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpg'
      const isJPEG = file.type === 'image/jpeg'
      const isPNG = file.type === 'image/png'
      const isLt1M = file.size / 1024 / 1024 < 1
      if (!isJPG && !isJPEG && !isPNG) {
        this.$message.error(this.$t('dashboard.impose1'))
      }
      if (!isLt1M) {
        this.$message.error(this.$t('dashboard.impose2'))
      }
      return isJPG && isJPEG && isPNG && isLt1M
    },
    // 修改
    edit(num) {
      const self = this
      if (num === 1) {
        self.showInput = true
      } else {
        self.showInput = false
      }
    },
    // 提交
    submit(formName) {
      // console.log(self.form)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$message.success(this.$t('dashboard.suresub'))
        } else {
          this.$message.error(this.$t('dashboard.errMessage'))
          return false
        }
      })
      // if (self.form.oldpass === '') {
      //   this.$message.error(this.$t('dashboard.sureoldpass'))
      // }
      // if (self.form.newpass === '') {
      //   this.$message.error(this.$t('dashboard.enterpass'))
      // } else if (self.form.surepass === '') {
      //   this.$message.error(this.$t('dashboard.confirmpassA'))
      // } else if (self.form.newpass !== self.form.surepass) {
      //   this.$message.error(this.$t('dashboard.twopass'))
      // } else {
      //   this.$message.success(this.$t('dashboard.suresub'))
      // }
    }
  }
}
</script>

<style scoped>
.content{
  background-color: #fff;
  padding: 30px 20px 20px 20px;
  height: 80vh;
}

#icon {
  margin-bottom: 5px;
}
.content >>> .el-upload--picture-card {
  border: 1px solid #ccc!important;
  border-radius: 50%!important;
  overflow: hidden;
}
.ipt {
  margin-top: 20px;
}
.content >>>.el-form-item__error {
  left: 20px!important;
}
.avatar {
  width: 140px;
  height: 140px;
  display: block;
}
</style>
